package androidUtil;

import java.io.*;

public class iOS切图命名过滤至Android规格 {
    private static final File resFolder = new File("F:\\TencentQQ\\2425875132\\FileRecv\\2018-11-23 趣味星球1.0\\切图PNG");
    private static final File destFolder = new File("F:\\星球切图");

    public static void main(String[] args) {
        for (File f :
                resFolder.listFiles()) {
            if (f.isDirectory())
                renameAndCopyToAndroidFolder(f);
        }
    }

    private static void renameAndCopyToAndroidFolder(File f) {
        File[] files = f.listFiles();
        for (File fileItem : files
                ) {
            String fileName = fileItem.getName();
            int index = -1;
            if ((index = fileName.indexOf("@2x")) != -1) {
                writeToDrawableFolder(f.getName(), "drawable-xhdpi", fileItem, fileName.substring(0, index) + ".png");
            } else if ((index = fileName.indexOf("@3x")) != -1) {
                writeToDrawableFolder(f.getName(), "drawable-xxhdpi", fileItem, fileName.substring(0, index) + ".png");
            } else {
//                writeToDrawableFolder(f, "drawable-mdpi", fileItem, fileName.substring(0, index) + ".png");
            }

        }
    }

    private static void writeToDrawableFolder(String subFolderName, String s, File fileItem, String fileName) {
        File destFile = new File(destFolder, subFolderName + File.separator + s + File.separator + fileName);


        try {
            File parentFile = destFile.getParentFile();
            if (!parentFile.exists())
                parentFile.mkdirs();
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fileItem));
            byte[] buf = new byte[4096];
            int len;
            while ((len = bis.read(buf)) != -1) {
                bos.write(buf, 0, len);
                bos.flush();

            }
            bos.close();
            bis.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
